﻿using JwtToken.Redis.WebApi.Models;
using JwtToken.Redis.WebApi.Tools;
using JwtToken.Redis.WebApi.ViewModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace JwtToken.Redis.WebApi.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthorizeController : ControllerBase
    {
        private JwtSettings _jwtSettings;

        public AuthorizeController(IOptions<JwtSettings> _jwtSettingsAccesser)
        {
            _jwtSettings = _jwtSettingsAccesser.Value;
        }
        /// <summary>
        /// 登录获取token
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Login([FromBody]LoginViewModel viewModel)
        {
            if (ModelState.IsValid)//判断是否合法
            {
                if (!(viewModel.User == "zzz" && viewModel.Password == "123456"))//判断账号密码是否正确
                {
                    return new Result() { code = 0, Msg = "账号验证错误" };
                }
                //return Ok(new { Access_Token = JwtTokenHelper.CreatToken(_jwtSettings, new Dto.LoginDataDto { UserName = "zzz", UserRoleName = "fd" }) });
                return new Result { code = 1, Msg = "success", Data = new { Access_Token = JwtTokenHelper.CreatToken(_jwtSettings, new Dto.LoginDataDto { UserName = "zzz", UserRoleName = "fd" }) } };

            }
            return new Result { code = -1, Msg = "请求错误", Type = "400" };
        }



    }
}